package com.clas.liu.myapplication.view.activity.impl;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clas.liu.myapplication.MyApplication;
import com.clas.liu.myapplication.R;
import com.clas.liu.myapplication.model.entity.user.UserData;
import com.clas.liu.myapplication.presenter.impl.BasePresenter;
import com.clas.liu.myapplication.view.util.SaveUtil;
import com.clas.liu.myapplication.view.widget.CircleImageView;
import com.hyphenate.chat.EMClient;
import com.squareup.picasso.Picasso;

/**
 * Created by liu on 2019/5/13.
 */

public class SettingActivity extends BaseActivity implements View.OnClickListener{

    private ImageView back;
    private CircleImageView head;
    private RelativeLayout editLayout;
    private Button loginOutBt;
    private TextView name;
    private TextView phone;
    private RelativeLayout editPdLayout;
    private RelativeLayout rootLayout;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting_layout);
        findView();
        init();
        setListener();
    }

    private void init() {
        UserData userData = SaveUtil.<UserData>getFromLocal(getApplicationContext(), "user");
        Picasso.with(this).load(userData.getHead()).placeholder(R.mipmap.default_head).error(R.mipmap.loading).into(head);
        name.setText(userData.getName());
        phone.setText(userData.getPhone());
        if (userData.getRole()==0)
            rootLayout.setVisibility(View.VISIBLE);
    }

    private void findView() {
        back = findViewById(R.id.setting_back_bt);
        head = findViewById(R.id.setting_user_head);
        editLayout = findViewById(R.id.setting_edit_user_info_layout);
        loginOutBt = findViewById(R.id.setting_login_out_bt);
        name = findViewById(R.id.setting_name);
        phone = findViewById(R.id.setting_phone);
        editPdLayout = findViewById(R.id.setting_edit_pd);
        rootLayout = findViewById(R.id.setting_root_layout);
    }

    private void setListener() {
        back.setOnClickListener(this);
        editLayout.setOnClickListener(this);
        loginOutBt.setOnClickListener(this);
        editPdLayout.setOnClickListener(this);
        rootLayout.setOnClickListener(this);
    }

    private void loginout() {
        SaveUtil.clearUserInfo(this);
        EMClient.getInstance().logout(true);
        startActivity(new Intent(this,LoginActivity.class));
        ((MyApplication)getApplication()).removeALLActivity_();
    }
    @Override
    protected BasePresenter getPresenter() {
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 102) {
            init();
            setResult(102);
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.setting_back_bt:
                finish();
                break;
            case R.id.setting_edit_user_info_layout:
                intent = new Intent(this,EditUserInfoActivity.class);
                startActivityForResult(intent,100);
                break;
            case R.id.setting_login_out_bt:
                loginout();
                break;
            case R.id.setting_edit_pd:
                intent = new Intent(this,EditPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.setting_root_layout:
                intent = new Intent(this,AdministratorActivity.class);
                startActivity(intent);
                break;
        }
    }


}
